<?php

namespace app\admin\common\controller;

use app\admin\framework\api\Permission;
use app\common\model\Order;
use app\common\model\OrderBoardItem;
use app\common\model\OrderTrip;
use app\common\service\OrderService;
use app\common\service\UserService;
use app\common\utils\Excel;
use think\facade\Db;

#[Permission('超时订单', 'Order', 10)]
class TimeoutorderController
{
    private $deptId = 0;
    
    public function __construct()
    {
        $this->deptId = UserService::getValue('dept_id');
    }
    
    
    #[Permission('参数')]
    public function option()
    {
        return apiSuccess(data: [
            'status' => Order::STATUS
        ]);
    }
    
    #[Permission('查询')]
    public function select()
    {
        $keyword = request()->get('keyword', '');
        $orderStatus = request()->get('orderStatus');
        $start_dept_id = request()->get('start_dept_id');
        $end_dept_id = request()->get('end_dept_id');
        $field = '*,"" as currentDept';

        $where[] = ['order_sn|accept_mobile|shipper_mobile|shipper_name|accept_name|address|end_address|car_shelf_num','like','%'.$keyword.'%'];

        $where[] = ['status', '<', 4];

        $where[] = ['eta', '<', date('Y-m-d')];

        $userInfo = UserService::get();
        $roles = UserService::getRoleIds($userInfo['id']);
        if(!in_array(1,$roles)){
            if ($userInfo['dept_id'] != 91) {
                $where[] = ['dept_id', '=', $userInfo['dept_id']];
            }
        }

        // 数据权限
        $dataAuth = UserService::getRoleDataAuth($userInfo['id']);
        if (!empty($dataAuth)) {
            $where[] = ['dept_id', 'in', $dataAuth];
        }

        $timeBetween = request()->get('timeBetween',[]);
        if(!empty($timeBetween)){
            $where[] = ['create_time','between',$timeBetween];
        }
        if(isset($orderStatus) && $orderStatus >= 0){
            $where[] = ['status', '=', $orderStatus];
        }
        if(isset($start_dept_id) && !empty($start_dept_id)){
            $where[] = ['dept_id', '=', $start_dept_id];
        }
        if(isset($end_dept_id) && !empty($end_dept_id)){
            $where[] = ['end_dept_id', '=', $end_dept_id];
        }
        $list = Order::where($where)
            ->field($field)
            ->with(['dept','endDept','insure'])
            ->order('id desc')
            ->paginate()
            ->each(function($item){
                if (isset($item->status)) {
                    $item->order_status_text = Order::STATUS[$item->status];
                } else {
                    $item->order_status_text = '';
                }
                if (isset($item->contract) && !empty($item->contract)) {
                    if (strpos($item->contract, 'http://sc.dm.ccjwcm.com') !== false) {
                        $item->contract = str_replace('http://sc.dm.ccjwcm.com', 'http://admin.nmshicheng.com', $item->contract);
                    }
                }
                $currentDept = OrderBoardItem::where(['order_id'=>$item->id])->with(['dept'])->order("update_time desc")->find();
                if (!empty($currentDept)) {
                    $item->currentDept = $currentDept["dept_name"];
                }
            });
        return apiSuccess(data: ['list' => $list->items(), 'total' => $list->total()]);
    }
}